<?php
/**
 * Created by PhpStorm.
 * User: woody
 * Date: 17-2-16
 * Time: 下午10:46
 */

namespace App\Http\Controllers\Admin;

use App\Models\Banner;
use Illuminate\Http\Request;

class BannerController extends AdminController {

    public function __construct()
    {
        parent::__construct();
        $this->_response['_active']['_model']       = 'banner';
        $this->_response['_active']['_action']      = '';
        $this->_response['_title']                  = '小一农货-广告图管理';
    }

    public function index(Request $request){
        $search = $request->all();
        $page = $request->get('page', 1);
        $this->_response['_active']['_action']      = 'list';

        $bannerModel = new Banner();
        $data = $bannerModel->getData($search ,$page, 20);

        $this->_response['banner'] = $data;

        $this->_response['banner']['pageData']['page'] = $page;
        $this->_response['banner']['pageData']['pageHtml'] = self::getPageHtml($page , $this->_response['banner']['pageData']['lastPage'] , '/admin/banner?');

        return view('admin.banner.list', $this->_response);
    }

    public function add(){
        $this->_response['_active']['_action']      = 'add';
        return view('admin.banner.add', $this->_response);
    }

    public function doAdd(\App\Http\Requests\Banner $request){
        $data = $request->all();
        $saveData = [
            'title' => $data['title'],
            'img' => $data['img'],
            'redirect_url' => $data['redirect_url'],
            'sort' => $data['sort'],
            'on_line_time' => $data['on_line_time'],
            'off_line_time' => $data['off_line_time'],
            'created_at' => date('Y-m-d H:i:s', time())
        ];

        $bannerModel = new Banner();
        if($bannerModel->saveData($saveData)){
            return response()->json(['code' => 0, 'msg' => '添加成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '添加失败']);
        }
    }
    public function delete($id){
        $Del =[
            'deleted_at' => date('Y-m-d H:i:s', time())
        ];
        $bannerModel = new Banner();
        if($bannerModel->Del($Del,$id)){
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '删除失败']);
        }
    }

    public function edit($id){
        $this->_response['banner'] = Banner::where('id', $id)->first();
        $this->_response['_active']['_action']      = 'edit';
        return view('admin.banner.edit', $this->_response);
    }

    public function doEdit(\App\Http\Requests\Banner $request){
        $data = $request->all();
      /*  print_r($data);
        exit;*/
        $saveEdit = [
            'id' => $data['id'],
            'title' => $data['title'],
            'img' => $data['img'],
            'redirect_url' => $data['redirect_url'],
            'sort' => $data['sort'],
            'on_line_time' => $data['on_line_time'],
            'off_line_time' => $data['off_line_time'],
            'created_at' => date('Y-m-d H:i:s', time())
        ];

        $bannerModel = new Banner();
        if($bannerModel->saveEdit($saveEdit,$data['id'])){
            return response()->json(['code' => 0, 'msg' => '修改成功']);
        }else{
            return response()->json(['code' => -1, 'msg' => '修改失败']);
        }

    }
}